package com.hula.onion.book.server.spider.platforms.lujinsuo;

import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.RegexParserUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class LuJinSuoSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(16);

        map.put("userName", form.getUserName());
        map.put("password", form.getPassword());
        map.put("validNum", form.getTicket());

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public AccountInfo parseAccountBalance(String html) {

        AccountInfo accountInfo = new AccountInfo();

        String beginRegex = "<span class=\"total-asset-number security-mark-hide\">";
        String endRegex = "</span>元";
        String result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();

        accountInfo.setAmount(new BigDecimal(result));

        return accountInfo;
    }


}
